home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Thread Manager / ThreadUtilities / SnakesWithSemaphores / ThreadUtil.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-17  |  5.6 KB  |  148 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ThreadUtil.h
  3.  
  4.     Contains:    External Interface to Thread Manager Utilities
  5.  
  6.     Written by:    Brad Post, Eric Anderson and Bill Knott
  7.  
  8.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                 4/19/93        bsp        Added the Boolean busy to the LinkedList Semaphore model.  This is because
  13.                                     we were getting interrupted while releasing the semaphore and appending some
  14.                                     one to the list of people waiting....
  15.                 3/3/93        bsp        Added a lastInLine element for the LinkedListSemaphore model.
  16.                 2/16/93        bsp        Added the prototype for InitializeThreadUtilities.
  17.                 2/9/93        bsp        Cleaned up the interface.
  18.                 2/8/93        bsp        Changed waitList to a **, made all functions PASCAL.  Changed
  19.                                     prototypes to use anArraySemaphorePtr as well as pascal.
  20.                 2/5/93        bsp     Added prototypes for CreateLinkedListSemaphore, DeleteLinkedListSemaphore,
  21.                                     DeleteSimpleSemaphore and CreateSimpleSemaphore.  Added the *aLinkedListSemaphorePtr,
  22.                                     *aSimpleSemaphorePtr, and *anArraySemaphorePtr.
  23.                 2/4/93        bsp        reversed the meaning of isAvailable and notAvailabe to work
  24.                                     with our TestAndSetByte routine
  25.                 2/2/93        bsp        Added prototypes and definitions for handling semaphores
  26.                 1/31/93        ewa        New Today.
  27.  
  28. */
  29.  
  30. #ifndef __THREADUTIL__
  31. #define __THREADUTIL__
  32.  
  33. #include    <Timer.h>
  34. #include    <Threads.h>
  35.  
  36. /* **********************************************************
  37.  * Defines used by this library 
  38.  * **********************************************************
  39.  */
  40.  
  41. /* **********************************************************
  42.  * Here are some of the error codes that can be returned. 
  43.  * **********************************************************
  44.  */
  45.  
  46. #define semaphoreButtHeadErr    1    // If there is something stupid going on
  47. #define semaphoreWaitErr        2   // Returned if you try to delete a semaphore and there are threads still waiting for it
  48.  
  49.  
  50. /* **********************************************************
  51.  * Structs & typedefs used by this library 
  52.  * **********************************************************
  53.  */
  54.  
  55. /* **********************************************************
  56.  * These typedefs are used only by the SimpleSemaphore model 
  57.  * **********************************************************
  58.  */
  59.  
  60. typedef Boolean SimpleSemaphore;
  61. typedef SimpleSemaphore *SimpleSemaphorePtr;
  62.  
  63. /* ***************************************************************************
  64.  * These typedefs and structs are used only by the LinkedList Semaphore model 
  65.  * ***************************************************************************
  66.  */
  67.  
  68. struct LinkedListElement {
  69.     ThreadID                    whoAmI;        
  70.     struct LinkedListElement    *next;            // Next thread waiting for semaphore
  71. };
  72.  
  73. typedef struct LinkedListElement LinkedListElement;
  74. typedef LinkedListElement *LinkedListElementPtr;
  75.  
  76. struct LinkedListSemaphore
  77. {
  78.     Boolean                    available;            // Is the semaphore available
  79.     Boolean                    busy;                // Are we busy trying to get the semaphore or releasing it???
  80.     ThreadID                theHolder;            // Who is the current holder
  81.     LinkedListElementPtr    waitingList;        // List of Threads waiting for Semaphore
  82.     LinkedListElementPtr    lastInList;            // Last thread waiting
  83. };
  84.  
  85. typedef struct LinkedListSemaphore LinkedListSemaphore;
  86. typedef LinkedListSemaphore *LinkedListSemaphorePtr;
  87.  
  88. /* **************************************************************************
  89.  * These typedefs and structs are only used by the Array Semaphore model 
  90.  * **************************************************************************
  91.  */
  92.  
  93. struct ArraySemaphore
  94. {
  95.     Boolean                available;            // is the semaphore available
  96.     ThreadID            theHolder;            // who is the current holder
  97.     long                whoseNext;            // whose next to be run
  98.     long                 nextSpot;            // next spot in the List to place a person 
  99.     long                numberWaiting;        // number of Threads waiting
  100.     long                maxInQueue;            // maximum number of Threads in the queue
  101.     ThreadID            **waitList;            // ID's of the number waiting
  102. };
  103.  
  104. typedef struct ArraySemaphore ArraySemaphore;
  105. typedef ArraySemaphore *ArraySemaphorePtr;
  106.  
  107.  
  108. /* **********************************************************
  109.  * Routine prototypes 
  110.  * **********************************************************
  111.  */
  112.  
  113. pascal void     InitializeThreadUtilities();
  114.  
  115. pascal void         DelayThread (unsigned long millisecDelay);
  116.  
  117. /* **********************************************************
  118.  * These are the rountines for using Simple Semaphores 
  119.  * **********************************************************
  120.  */
  121.  
  122. pascal OSErr CreateSimpleSemaphore( SimpleSemaphorePtr *aSemaphore );
  123. pascal OSErr GetSimpleSemaphore( SimpleSemaphorePtr aSemaphore );
  124. pascal OSErr ReleaseSimpleSemaphore( SimpleSemaphorePtr aSemaphore );
  125. pascal OSErr DeleteSimpleSemaphore( SimpleSemaphorePtr aSemaphore );
  126.  
  127. /* **********************************************************
  128.  * These are the rountines for using LinkedList Semaphores 
  129.  * **********************************************************
  130.  */
  131.  
  132. pascal OSErr CreateLinkedListSemaphore( LinkedListSemaphorePtr *aSemaphore );
  133. pascal OSErr GetLinkedListSemaphore( LinkedListElement *whichOne, LinkedListSemaphorePtr aSemaphore );
  134. pascal OSErr ReleaseLinkedListSemaphore( LinkedListSemaphorePtr aSemaphore );
  135. pascal OSErr DeleteLinkedListSemaphore( LinkedListSemaphorePtr aSemaphore);
  136.  
  137. /* **********************************************************
  138.  * These are the rountines for using Array Semaphores 
  139.  * **********************************************************
  140.  */
  141.  
  142. pascal OSErr CreateArraySemaphore(long maxInQueue, ArraySemaphorePtr *aSemaphore);
  143. pascal OSErr GetArraySemaphore( ArraySemaphorePtr aSemaphore );
  144. pascal OSErr ReleaseArraySemaphore( ArraySemaphorePtr aSemaphore);
  145. pascal OSErr DeleteArraySemaphore( ArraySemaphorePtr aSemaphore );
  146.  
  147. #endif /* __THREADUTIL__ */
  148.